﻿using Dev.DevNet.IServices;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Dev.DevNet.Services
{
    public class DbBaseService<T> : IDbBaseService<T> where T : class, new()
    {
        protected ISqlSugarClient _sqlSugarClient;
        public DbBaseService(ISqlSugarClient sqlSugarClient)
        {
            this._sqlSugarClient = sqlSugarClient;
        }
        ///// <summary>
        ///// 获取数据库访问对象
        ///// </summary>
        ///// <returns></returns>
        //public SqlSugarClient CreateDbInstance()
        //{
        //    SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
        //    {
        //        ConnectionString = "连接符字串",
        //        DbType = DbType.SqlServer,
        //        IsAutoCloseConnection = true
        //    });
        //    db.Aop.OnLogExecuting = (sql, pra) =>
        //    {
        //        Console.WriteLine("********************************************");
        //        Console.WriteLine($"Sql语句：{sql}");
        //    };
        //    return db;

        //}
        public int Add(T t)
        {
            return _sqlSugarClient.Insertable<T>(t).ExecuteCommand();
            
        }
        public int AddReturnIdentity(T t)
        {
            return _sqlSugarClient.Insertable<T>(t).ExecuteReturnIdentity();
        }



        public int Delete(T t)
        {
            return _sqlSugarClient.Deleteable(t).ExecuteCommand();
        }

        public int Delete(Expression<Func<T, bool>> expression)
        {
            return _sqlSugarClient.Deleteable(expression).ExecuteCommand();
        }

        public int Delete(int Id)
        {
            throw new NotImplementedException();
        }

        public int Update(T t)
        {
            return _sqlSugarClient.Updateable(t).ExecuteCommand();
        }

        public int Update(List<T> UpdateObjs)
        {
            return _sqlSugarClient.Updateable(UpdateObjs).ExecuteCommand();
        }

        public ISugarQueryable<T> QueryWhere(Expression<Func<T, bool>> expression)
        {
          return _sqlSugarClient.Queryable<T>().Where(expression);
        }
    }
}
